﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.SoQuy
{
    public partial class frmKetChuyen : Form
    {
        #region Khai báo
        string makho = "";
        string tenkho = "";
        bool checkClose = false;
        bool check = false;
        string ma = "";
        string id = "";
        DateTime date;
        string nam = "";
        string thang = "";
        Entities.SoDuSoQuy[] hienthi;

        public frmKetChuyen()
        {
            InitializeComponent();
            Entities.CaiDatKho kho = Entitystatic.ViewFile();
            makho = kho.MaKho;
            tenkho = kho.TenKho;
            date = Entitystatic.date;
            cbbNam.Text = date.Year.ToString();
            cbbThang.Text = date.Month.ToString();
            nam = date.Year.ToString();
            thang = date.Month.ToString();
            SelectData();
        }

        public frmKetChuyen(string makho, int month1, int year1)
        {
            InitializeComponent();
            this.makho = makho;
            date = DateTime.Parse(month1 + "/01/" + year1);
            cbbNam.Text = date.Year.ToString();
            cbbThang.Text = date.Month.ToString();
            nam = date.Year.ToString();
            thang = date.Month.ToString();
            SelectData();
        }
        #endregion

        #region select
        void SelectData()
        {
            try
            {
                hienthi = Common.StaticVariable.InterfaceObject.SelectSoQuy(makho);
                if (hienthi == null)
                {
                    lbTrangThai.Text = "Kỳ này chưa đóng";
                    lbTrangThai.ForeColor = Color.Black;
                    btnKetChuyen.Enabled = true;
                    return;
                }
                nam = cbbNam.Text;
                thang = cbbThang.Text;
                Entities.SoDuSoQuy[] sq = new Entities.SoDuSoQuy[hienthi.Length];
                int sotang = 0;
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = hienthi[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi[i].NgayKetChuyen.Month.ToString();

                    if (namkt == nam && thangkt == thang)
                    {
                        if (hienthi[i].TrangThai == false)
                        {
                            lbTrangThai.Text = "Kỳ này chưa đóng";
                            lbTrangThai.ForeColor = Color.Black;
                            btnKetChuyen.Enabled = true;
                        }
                        else
                        {
                            lbTrangThai.Text = "Kỳ này đã khóa";
                            lbTrangThai.ForeColor = Color.Red;
                            btnKetChuyen.Enabled = true;
                        }
                        id = hienthi[i].SoDuSoQuyID.ToString();
                        sq[sotang] = hienthi[i];
                        sotang++;
                        break;
                    }
                    else
                    {
                        lbTrangThai.Text = "Kỳ này chưa đóng";
                        lbTrangThai.ForeColor = Color.Black;
                        btnKetChuyen.Enabled = true;
                    }

                }
                if (sotang != 0)
                {
                    hienthi = new Entities.SoDuSoQuy[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi[i] = sq[i];
                    }

                }
                else
                    hienthi = null;
            }
            catch
            {
            }
        }
        #endregion

        #region kết chuyển
        public void KetChuyen()
        {
            string monthhientai = date.Month.ToString();
            string namhientai = date.Year.ToString();

            if (hienthi != null)
            {
                string month = hienthi[0].NgayKetChuyen.Month.ToString();
                string year = hienthi[0].NgayKetChuyen.Year.ToString();
                if (month == monthhientai && year == namhientai)
                {
                    SoQuy.frmSoQuy a = new SoQuy.frmSoQuy(year, month);
                    if (int.Parse(month) == 12)
                    {
                        month = "1";
                        year = (int.Parse(year) + 1).ToString();
                    }
                    else
                    {
                        month = (int.Parse(monthhientai) + 1).ToString();
                    }
                    Entities.SoDuSoQuy pt = new Entities.SoDuSoQuy();
                    pt.HanhDong = "UpdateTrangThai";
                    pt.SoDuSoQuyID = int.Parse(id);
                    pt.MaSoDuSoQuy = getID();
                    pt.NgayKetChuyen = Convert.ToDateTime(month + "/01/" + year);
                    pt.SoDuDauKy = frmSoQuy.a;
                    pt.SoDuCuoiKy = "0";
                    pt.MaKho = makho;
                    if (Common.StaticVariable.InterfaceObject.UpdateTrangThaiSoQuy(pt) == 0)
                    {
                        return;
                    }
                    SelectData();
                }
                else
                {
                    string monthkytruoc = "";
                    string yearkytruoc = "";
                    if ((DateTime.Now.Month - 1) == 0)
                    {
                        monthkytruoc = "12";
                        yearkytruoc = (DateTime.Now.Year - 1).ToString();
                    }
                    else
                    {
                        monthkytruoc = (DateTime.Now.Month - 1).ToString();
                        yearkytruoc = DateTime.Now.Year.ToString();
                    }
                    if (month == "12" && year == yearkytruoc)
                    {
                        frmSoQuy a = new frmSoQuy(year, month);
                        Entities.SoDuSoQuy pt = new Entities.SoDuSoQuy();
                        pt.HanhDong = "UpdateTrangThai";
                        pt.SoDuSoQuyID = int.Parse(id);
                        pt.MaSoDuSoQuy = getID();
                        pt.NgayKetChuyen = Convert.ToDateTime("01/01/" + (int.Parse(yearkytruoc) + 1).ToString());
                        pt.SoDuDauKy = frmSoQuy.a;
                        pt.SoDuCuoiKy = "0";
                        pt.MaKho = makho;
                        if (Common.StaticVariable.InterfaceObject.UpdateTrangThaiSoQuy(pt) == 0)
                        {
                            return;
                        }
                        SelectData();
                        return;
                    }
                    if (month == monthkytruoc && year == yearkytruoc)
                    {
                        frmSoQuy a = new frmSoQuy(year, month);
                        Entities.SoDuSoQuy pt = new Entities.SoDuSoQuy();
                        pt.HanhDong = "UpdateTrangThai";
                        pt.SoDuSoQuyID = int.Parse(id);
                        pt.MaSoDuSoQuy = getID();
                        pt.NgayKetChuyen = Convert.ToDateTime((int.Parse(monthkytruoc) + 1).ToString() + "/01/" + yearkytruoc);
                        pt.SoDuDauKy = frmSoQuy.a;
                        pt.SoDuCuoiKy = "0";
                        pt.MaKho = makho;
                        if (Common.StaticVariable.InterfaceObject.UpdateTrangThaiSoQuy(pt) == 0)
                        {
                            return;
                        }
                        SelectData();
                    }
                }
            }
            else
            {
                string month = cbbThang.Text;
                string year = cbbNam.Text;
                if (month == monthhientai && year == namhientai)
                {
                    frmSoQuy a = new frmSoQuy(year, month);
                    string masd = getID();
                    Entities.SoDuSoQuy pt1 = new Entities.SoDuSoQuy();
                    pt1 = new Entities.SoDuSoQuy();
                    pt1.HanhDong = "Insert";
                    pt1.MaSoDuSoQuy = masd;
                    pt1.SoDuDauKy = "0";
                    pt1.NgayKetChuyen = Convert.ToDateTime(month + "/01/" + year);
                    pt1.SoDuCuoiKy = frmSoQuy.a;
                    pt1.TrangThai = false;
                    pt1.MaKho = makho;
                    if (Common.StaticVariable.InterfaceObject.InsertSoQuy(pt1) == 0)
                    {
                        return;
                    }
                    SelectData();
                    KetChuyen();

                }
            }

        }

        public string getID()
        {
            try
            {
                string str = "";
                Entities.GetID layid = new Entities.GetID();
                layid.TableName = "SoDuSoQuy";
                Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);
                if (lay.ColumnName == null)
                {
                    return "SDSQ_0001";
                }
                str = Common.Utilities.ProcessID(lay.ColumnName);
                return str;
            }
            catch
            {
                return "SDSQ_0001";
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmKetChuyen_Load(object sender, EventArgs e)
        {
            lblTieuDe.Text = "Kết Chuyển Số Dư Sổ Quỹ Kỳ " + date.Month + "/" + date.Year;
            lbChuY.Text = "Hãy chắc chắn hôm nay " + date.Day + "/" + date.Month + "/" + date.Year + "\nlà ngày giao dịch cuối cùng trong kỳ";
            this.FormClosing += new FormClosingEventHandler(frmKetChuyen_FormClosing);
            btnKetChuyen.Click += new EventHandler(btnKetChuyen_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            cbbNam.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged);
            cbbThang.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged);
        }

        void cbb_SelectedIndexChanged(object sender, EventArgs e)
        {
            nam = cbbNam.Text;
            thang = cbbThang.Text;
            lblTieuDe.Text = "Kết Chuyển Số Dư Sổ Quỹ Kỳ " + cbbThang.Text + "/" + cbbNam.Text;
            SelectData();
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnKetChuyen_Click(object sender, EventArgs e)
        {
            KetChuyen();
        }

        void frmKetChuyen_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion